//
//  STAdManager.swift
//  iPhone
//
//  Created by Toj on 3/31/23.
//

import Foundation

public class STAdManager: NSObject {
    static let manager = STAdManager()
    
    private var _appOpenAdmobModel: STAdmobModel?
    private var _rewardAdmobModel: STAdmobModel?
}

public extension STAdManager {
    
    /* 获取 缓存广告 数据 */
    func appOpenAdmobModelCache() -> STAdmobModel? {
        if _appOpenAdmobModel == nil {
            let json = STDiskManager.manager.cache(key: .preloadCacheAdJsonAppOpen)
            //print("-->toj: 开屏:get 缓存json:\(json)")
            print("-->toj: 开屏:get 缓存json")
            guard !json.isEmpty else { return nil }
            
            guard let admobModel = json.json_deserialize(model: STAdmobModel.self) else { return nil }
            _appOpenAdmobModel = admobModel
            print("-->toj: 开屏:get 缓存json反序列化成功M: \(admobModel.model)")
        }
        return _appOpenAdmobModel
    }
    
    /* 设置 缓存广告 数据 */
    func setAppOpenAdmobModelCache(admobModel: STAdmobModel?) {
        _appOpenAdmobModel = admobModel
        
        let json = admobModel?.toJSONString() ?? ""
        // print("-->toj: 开屏:set 固持久化JSON数据: \(json)")
        STDiskManager.manager.setCache(key: .preloadCacheAdJsonAppOpen, value: json)
        guard let adModel = admobModel else { return }
        print("-->toj: 开屏:set 固持久化JSON数据M: \(adModel.model.rawValue)")
    }
    
    func requestAppOpenAds(completion: @escaping ((_ admobModels: [STAdmobModel])->Void)) {
        var random = arc4random_uniform(5)
        random = 3
        print("-->toj: Ad random:\(random)")
        var json: [String: Any] = [:]
        if random == 0 { json = nav_openAdJSON }
        if random == 1 { json = adm_openAdJSON }
        if random == 2 { json = adm_openAdsJSON }
        if random == 3 { json = adm_insertAdJSON }
        if random == 4 { json = adm_rewardAdJSON }
        // json = [:]
        let result = json.json_deserialize(model: STResponse<[STAdmobModel]>.self)
        let admobModels = result?.data ?? []
        completion(admobModels)
    }
}

public extension STAdManager {
    
    func requestInterstitialAd(completion: @escaping ((_ response: STAdmobModel?)->Void)) {
        let random = arc4random_uniform(2)
        var json: [String: Any] = [:]
        if random == 0 { json = nav_openAdJSON }
        if random == 1 { json = adm_insertAdJSON }
   
        let result = json.json_deserialize(model: STResponse<[STAdmobModel]>.self)
        let admobModel = result?.data?.first
        admobModel?.nativeType = .interstitial
        completion(admobModel)
    }
}

public extension STAdManager {
    
    /* 获取 激励广告缓存广告 数据 */
    func rewardAdmobModelCache() -> STAdmobModel? {
        if _rewardAdmobModel == nil {
            let json = STDiskManager.manager.cache(key: .preloadCacheAdJsonReward)
            print("-->toj: 激励广告:get 缓存json:\(json)")
            guard !json.isEmpty else { return nil }
            
            guard let admobModel = json.json_deserialize(model: STAdmobModel.self) else { return nil }
            _rewardAdmobModel = admobModel
            print("-->toj: 激励广告:get 缓存json反序列化成功")
        }
        return _rewardAdmobModel
    }
    
    /* 设置 缓存广告 数据 */
    func setRewardAdmobModelCache(admobModel: STAdmobModel?) {
        _rewardAdmobModel = admobModel
        
        let json = admobModel?.toJSONString() ?? ""
        print("-->toj: 激励广告:set 固持久化JSON数据: \(json)")
        STDiskManager.manager.setCache(key: .preloadCacheAdJsonAppOpen, value: json)
    }
}

public extension STAdManager {
    
    /** API: /adm/v1/negotiator/reward-ads GET
     *  上报观看记录并发放会员奖励
     *  subProgramId: 子节目id
     *  mediumId: 频道ID或节目ID
     *  mediumType: 媒介类型 0:点播 1:直播
     *  result: 广告列表
     */
     func requestReward(mediumId: String, subProgramId: String, mediumType: Int, _ completion: @escaping ((_ admobModels: [STAdmobModel])->Void)) {
        completion([STAdmobModel]())
        //        provider.sendRequest(.negotiatorReward(mediumId: mediumId, subProgramId: subProgramId, mediumType: mediumType)) { (result:STResult<STModel<[RewardADModel]>, MoyaError>) in
        //            let ads = result.value?.data ?? []
        //            completion(ads)
        //        }
    }
    
    /** API: /adm/v1/negotiator/ads/report POST
     *  上报观看记录并发放会员奖励
     *  positionId: 展示机会id
     *  strategy:   策略id
     *  outerId:    活动id
     */
   func requestRewardReport(admobModel: STAdmobModel, _ completion: @escaping ((_ report: STAdReportModel)->Void)) {
        //        let positionId = admobModel.position_id
        //        let strategy = admobModel.space_id
        //        let outerId = admobModel.outer_id
        
        let report = STAdReportModel()
        completion(report)
        //        provider.sendRequest(.negotiatorRewardReport(positionId: positionId, strategy: strategy, outerId: outerId)) { (result:STResult<STModel<ADRewardReportModel>, MoyaError>) in
        //            let model = result.value
        //            completion(model)
        //        }
    }
}

